package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.Feed;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;

public class FeedsPanel extends DialogBox {
    private FeedTable table;
    private FeedForm form;

    public FeedsPanel() {
	super(true);
	setText(Dictionary.getEntry(Dictionary.TITLE_OFFERED_FEEDS, this));
	addStyleName("body");
	setAnimationEnabled(true);
	FlexTable main = new FlexTable();
	this.table = new FeedTable(this);
	main.setWidget(0, 0, this.form = new FeedForm(this));
	main.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
	main.setWidget(0, 1, this.table);
	setWidget(main);
    }

    public Feed getFeed() {
	return this.form.getFeed();
    }

    public Long getFeedId() {
	return getFeed().getId();
    }

    public void refreshFeedsTable() {
	this.table.refreshTable();
    }

    public void refreshForm(Feed feed) {
	this.form.refresh(feed);
    }

    public void save(Feed feed) {
	Feeds.getService().saveFeed(feed, new AsyncCallback<Feed>() {

	    @Override
	    public void onFailure(Throwable caught) {
		Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_COMM_ERROR, null));
	    }

	    @Override
	    public void onSuccess(Feed result) {
		if (result != null) {
		    Feeds.feeds.put(result.getId(), result);
		}
		FeedsPanel.this.table.refreshTable();
		FeedsPanel.this.form.refresh(result);
	    }
	});
    }

}
